package com.breakfast.fun.ui.home;

import com.breakfast.fun.data.entity.home.Home;
import com.breakfast.fun.data.entity.home.Version;
import com.breakfast.fun.data.entity.home.product.ProductDetail;
import com.breakfast.fun.data.entity.home.product.ProductState;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by hp on 2016/8/16.
 */
public class HomePresenter extends HomeContract.Presenter {
    @Override
    public void getHome() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "home_info");
        showLoadingDialog(true);

        rxManager.add(model.getHomeModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Home>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        hideLoadingDialog();
                        view.showFailure();
                    }

                    @Override
                    public void onNext(Home home) {
                        hideLoadingDialog();
                        if (home.isSuccess()) {
                            view.addBanner(home.getData().getCenterAd());
                            view.addTypeIcon(home.getData().getProductClasses(), home.getData().getDailyActivity(), home.getData().getNotice(), home.getData().getNoticeTime());
                            view.addType(home.getData().getHotProduct(), home.getData().getNewProduct());
                            view.showAd(home.getData().getActivityAd());

//                            view.addGallery(home.getData().getProducts(), home.getData().getNotice(), home.getData().getMoreProductsTipImage());
                        } else {
                            T.show(home.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void refresh() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "home_info");

        showLoadingDialog(false);
        rxManager.add(model.getHomeModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Home>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        view.stopRefesh();
                        hideLoadingDialog();
                        //view.showFailure();
                    }

                    @Override
                    public void onNext(Home home) {
                        view.stopRefesh();
                        hideLoadingDialog();
                        if (home.isSuccess()) {
                            view.refreshData(home);
                            getProductState();
                        } else {
                            T.show(home.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void loadMore(int page) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "findHotProduct");
        map.put("page", page + "");
        rxManager.add(model.getProductModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductDetail>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        view.stopRefesh();
                        //view.showFailure();
                    }

                    @Override
                    public void onNext(ProductDetail productDetail) {
                        view.stopRefesh();
                        if (productDetail.isSuccess()) {
                            view.loadMoreProduct(productDetail);
                            if (productDetail.getData().size() == 0) {
                                T.show("没有更多数据了");
                            }
                        } else {
                            T.show(productDetail.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void checkVersion() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "version");

        rxManager.add(model.getVersionModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Version>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Version version) {
                        if (version.isSuccess()) {
                            view.updateVersion(version);
                        }
                    }
                }));
    }

    @Override
    public void getProductState() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "weekday");


        rxManager.add(model.getProductStateModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ProductState>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        T.show("获取数据失败");
                    }

                    @Override
                    public void onNext(ProductState productState) {
                        if (productState.isSuccess()) {
                            view.setProductState(productState);
                        } else {
                            T.show(productState.getMsg());
                        }
                    }
                }));
    }

    @Override
    public void onStart() {
        getHome();
        getProductState();
    }
}
